package com.LostAndFound.web.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.LostAndFound.domain.Root;
import com.LostAndFound.domain.SystemInfo;
import com.LostAndFound.service.SystemInfoService;
import com.LostAndFound.utils.CommonsUtils;
import com.google.gson.Gson;
public class SystemInfoServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	
	SystemInfoService service = new SystemInfoService();

	// 分页
	public void getInfoListForEasyUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 当前页
		int pageNumber = Integer.parseInt(request.getParameter("page"));
		// 每页总条数
		int pageSize = Integer.parseInt(request.getParameter("rows"));
		List<Map<String, Object>> mapList = service.getInfoListForEasyUI(pageNumber,pageSize);
		int total = service.getTotalInfoForEasyUI();
		Gson gson = new Gson();
		String rows = gson.toJson(mapList);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"total\":"+total+",\"rows\":"+rows+"}");
	}
	// 删除记录
	public void handleDeleteSystemInfoForEasyUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String infoid = request.getParameter("infoid");
		boolean success = service.handleDeleteSystemInfoForEasyUI(infoid);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"success\":"+success+"}");
	}
	// 发布新公告
	public void handleAddSystemInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取数据
		Integer rid = Integer.parseInt(request.getParameter("rid"));
		String title = request.getParameter("title");
		String infoDetail = request.getParameter("infoDetail");
		Date now = new Date();
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateTime = ft.format(now);
		// 封装systemInfo对象
		SystemInfo systemInfo = new SystemInfo();
		systemInfo.setInfoid(CommonsUtils.getTimeForId());
		systemInfo.setTitle(title);
		systemInfo.setInfoDetail(infoDetail);
		systemInfo.setDatetime(dateTime);
		Root root = new Root();
		root.setRid(rid);
		systemInfo.setRoot(root);
		boolean success = service.handleAddSystemInfo(systemInfo);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"success\":"+success+"}");
	}
	
}